package com.hmy.uaa.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.uaa.bo.request.ResourceRequest;
import com.hmy.uaa.bo.request.RoleResourceRequest;
import com.hmy.uaa.bo.response.ResourceResponse;
import com.hmy.uaa.mapper.ResourceMapper;
import com.hmy.uaa.po.Resource;
import com.hmy.common.exception.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 资源信息
 */
@Service
public class ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private RoleResourceRelationService roleResourceRelationService;

    /**
     * 新增资源
     *
     * @param resourceRequest 请求参数
     */
    public void save(ResourceRequest resourceRequest) {
        if (isExists(resourceRequest)) {
            throw new ClientException("该资源已存在");
        }
        Resource resource = convertBean(resourceRequest);
        resource.setCreateTime(LocalDateTime.now());
        resourceMapper.insert(resource);
    }


    /**
     * 删除资源
     *
     * @param id 资源id
     */
    public void delete(Long id) {


        //删除资源
        LambdaQueryWrapper<Resource> resourceLambdaQueryWrapper = Wrappers.<Resource>lambdaQuery()
                .eq(Resource::getId, id);
        resourceMapper.delete(resourceLambdaQueryWrapper);
    }

    /**
     * 查询资源
     *
     * @param menuId 菜单id
     * @return Page<ResourceResponse> 返回数据
     */
    public List<ResourceResponse> queryResourceList(Long menuId) {

        return resourceMapper.queryResourceList(menuId);
    }

    /**
     * 查询绑定资源
     *
     * @param roleId 角色id
     * @return Page<ResourceResponse> 返回数据
     */
    public List<ResourceResponse> queryBindResourceList(Long roleId) {

        return resourceMapper.queryResourceList(roleId);
    }

    /**
     * 绑定菜单
     *
     * @param roleResourceRequest 绑定参数
     */
    @Transactional
    public void bindRole(RoleResourceRequest roleResourceRequest) {

        //1、先删除绑定角色的资源
        roleResourceRelationService.delete(roleResourceRequest.getRoleId());

        //2、绑定角色
        roleResourceRelationService.insertBatch(roleResourceRequest);
    }

    /**
     * 验证资源在该菜单下是否存在
     *
     * @param resourceRequest 请求参数
     * @return boolean 返回结果
     */
    private boolean isExists(ResourceRequest resourceRequest) {
        LambdaQueryWrapper<Resource> queryWrapper = Wrappers.<Resource>lambdaQuery()
                .eq(Resource::getResourceKey, resourceRequest.getResourceKey());

        queryWrapper.eq(Resource::getMenuId, resourceRequest.getMenuId());
        if (Objects.nonNull(resourceRequest.getId())) {
            queryWrapper.ne(Resource::getId, resourceRequest.getId());
        }
        return resourceMapper.selectCount(queryWrapper) > 0;
    }

    /**
     * 转换bean
     *
     * @param resourceRequest 请求参数
     * @return Resource 返回转换对象
     */
    private Resource convertBean(ResourceRequest resourceRequest) {
        Resource resource = new Resource();

        resource.setMenuId(resourceRequest.getMenuId());
        resource.setDescription(resourceRequest.getDescription());
        resource.setResourceKey(resourceRequest.getResourceKey());
        resource.setResourceValue(resourceRequest.getResourceValue());
        return resource;
    }


}
